<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-json-lib.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<%
Response.ContentType = "application/json"
chks=request("chks")
act=request("act")
selwtr=SafeRequest("selwtr",1)
aChk=split(chks,",")
cd_update=Now()
sql=""

function OperateRecord(sql_act,re_table,re_id,re_operate,re_memo)
    dim aField,aFieldName,sSelectColumn,iFieldCount
    dim sql_sel,sql_opt,opt_id,rs_old,rs_new
    dim aaSourceValue,aaDestValue
    
    aField=dCdTable.keys
    aFieldName=dCdTable.items
    sSelectColumn=join(aField,",")
    iFieldCount=ubound(aField)
    
    ' 保存原始数据
    sql_sel="select "&sSelectColumn&" from cdfee inner join post on cdfee.cd_post=post.id where cdfee.id="&re_id
    set rs_old=server.createobject("adodb.recordset")
    rs_old.open sql_sel,conn,1,1
    if rs_old.bof and rs_old.eof then
        redim aaSourceValue(iFieldCount,0)
    else
        aaSourceValue=rs_old.getrows(,,aField)
    end if
    rs_old.close
    set rs_old=nothing


    ' 执行原数据表的sql操作，如果是insert操作，返回新插入的id，否则直接提交
    if lcase(left(ltrim(sql_act),6))="insert" then
        re_id=ExecuteCommand(sql_act,true)
    else
        call ExecuteCommand(sql_act,false)
    end if
    
    
    ' 执行主操作记录表
    sql_opt="insert into record(re_table,re_id,re_operate,re_memo,username,re_clientid) values('"&lcase(re_table)&"',"&re_id&",'"&re_operate&"','"&re_memo&"',"&session("iId")&","&session("iClientId")&")"
    opt_id=ExecuteCommand(sql_opt,true)
    
    '更新操作后马上读取新信息
    sql_sel="select "&sSelectColumn&" from cdfee inner join post on cdfee.cd_post=post.id where cdfee.id="&re_id
    set rs_new=server.createobject("adodb.recordset")
    rs_new.open sql_sel,conn,1,1
    if rs_new.bof and rs_new.eof then
        redim aaDestValue(iFieldCount,0)
    else
        aaDestValue=rs_new.getrows(,,aField)
    end if
    rs_new.close
    set rs_new=nothing
    
    
    '开始循环判断cd表的不同之处
    dim i
    for i=0 to iFieldCount
        if aaDestValue(i,0)<>aaSourceValue(i,0) then
            sql_record="insert into records(opt_id,res_source,res_dest,res_fieldname) values ("&opt_id&",'"&aaSourceValue(i,0)&"','"&aaDestValue(i,0)&"','"&aFieldName(i)&"')"
            call ExecuteCommand(sql_record,false)
        end if
    next
    
    
    OperateRecord=re_id
end function    


' response.write chks
'先清除由于conn.open而产生的警告以 Errors 集合中的 Error 对象的方式出现，但并不中止程序的执行
conn.errors.clear
'事务开始
conn.BeginTrans
' set cmd=Server.CreateObject("ADODB.Command")
' cmd.ActiveConnection=conn
if act="认领" then
    if CheckPostFlag(session("aaPostFlag"),2,4) then
        ' 读取委托人保管费模板
        dim wtr_receive,wtr_pay,aaWtrFeeTmp
        wtr_receive=0
        wtr_pay=0
        set rs_wtr=server.createobject("adodb.recordset")
        sql_wtr="SELECT iif(isnull(receive),0,receive) as wtr_receive,iif(isnull(pay),0,pay) as wtr_pay from wtr left join tmp on wtr.tmp=tmp.id where wtr.id="&selwtr
        rs_wtr.open sql_wtr,conn,1,1
        if not rs_wtr.bof and not rs_wtr.eof then
            wtr_receive=rs_wtr("wtr_receive")
            wtr_pay=rs_wtr("wtr_pay")
        end if
        rs_wtr.close
        
        ' 读取委托人详细费用模板
        sql_wtr="SELECT feeid,feer,feep from wtrfeetmp where wtrid="&selwtr
        rs_wtr.open sql_wtr,conn,1,1
        if rs_wtr.bof and rs_wtr.eof then
            redim aaWtrFeeTmp(3,0)
        else
            aaWtrFeeTmp=rs_wtr.getrows()
        end if
        rs_wtr.close
        set rs_wtr=nothing
    
        for i=0 to ubound(aChk)
            ' response.write aChk(i)
            aVal=split(aChk(i),"|")
            ' sql=""
            sql="update cd set wtr="&selwtr&",cd_post=2,cd_receive="&wtr_receive&",cd_pay="&wtr_pay&",cd_update='"&cd_update&"' where id="&aVal(0)
            ' call ExecuteCommand(sql,false)
            call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
            sql="delete * from fee where cd="&aVal(0)
            call ExecuteCommand(sql,false)
            for j=0 to ubound(aaWtrFeeTmp,2)
                if not IsEmpty(aaWtrFeeTmp(0,j)) then
                    sql="insert into fee(cd,detail,receive,pay) values("&aVal(0)&","&aaWtrFeeTmp(0,j)&","&aaWtrFeeTmp(1,j)&","&aaWtrFeeTmp(2,j)&")"
                    call ExecuteCommand(sql,false)
                    ' call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
                end if
            next
        next
    end if

end if
if act="已收" or act="已付" then
    if CheckPostFlag(session("aaPostFlag"),4,4) then
        for i=0 to ubound(aChk)
            ' response.write aChk(i)
            aVal=split(aChk(i),"|")
                ' sql=""
                if act="已收" and not aVal(1) then
                    sql="update cd set cd_received=true,cd_post=4,cd_update='"&cd_update&"',cd_actreceive="&aVal(2)&" where id="&aVal(0)
                    ' call ExecuteCommand(sql,false)
                    ' call OperateRecord("cd",aVal(0),"编辑",act&"-批量")
                    call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
                end if
                if act="已付" and not aVal(3) then
                    sql="update cd set cd_paid=true,cd_post=4,cd_update='"&cd_update&"',cd_actpay="&aVal(4)&" where id="&aVal(0)
                    ' call ExecuteCommand(sql,false)
                    ' call OperateRecord("cd",aVal(0),"编辑",act&"-批量")
                    call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
                end if
                '只能对没有进行过已收、已付操作的单子操作，防止重复覆盖
                ' if sql<>"" then
                    ' on error resume next
                    ' response.write sql
                    ' call ExecuteCommand(sql,false)
                    ' response.end
                    ' cmd.CommandText = sql
                    ' cmd.Execute
                    ' call OperateRecord("cd",aVal(0),"编辑",act&"-批量")
                ' end if
        next
    end if
end if
if act="确认" then
    if CheckPostFlag(session("aaPostFlag"),6,4) then
        for i=0 to ubound(aChk)
            aVal=split(aChk(i),"|")
            sql="update cd set cd_confirm=true,cd_update='"&cd_update&"' where id="&aVal(0)
            call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
        next
    end if
end if
if act="隐藏" then
    if CheckPostFlag(session("aaPostFlag"),1,4) then
        for i=0 to ubound(aChk)
            aVal=split(aChk(i),"|")
            sql="update cd set cd_hidden=true,cd_update='"&cd_update&"' where cd_post=1 and id="&aVal(0)
            call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
        next
    end if
end if
if act="显示" then
    if CheckPostFlag(session("aaPostFlag"),1,4) then
        for i=0 to ubound(aChk)
            aVal=split(aChk(i),"|")
            sql="update cd set cd_hidden=false,cd_update='"&cd_update&"' where cd_post=1 and id="&aVal(0)
            call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
        next
    end if
end if
if act="核销" then
    if CheckPostFlag(session("aaPostFlag"),5,4) then
        for i=0 to ubound(aChk)
            aVal=split(aChk(i),"|")
            'cd_status<=2 是防止多次点击 核销按钮，导致出现4
            sql="update cd set cd_status=cd_status+1,CD_STATUSDATE='"&date()&"',cd_update='"&cd_update&"' where cd_status<=2 and id="&aVal(0)
            call OperateRecord(sql,"cd",aVal(0),"编辑",act&"-批量")
        next
    end if
end if
' set cmd=nothing
'判断没有任何sql执行过，就清楚错误信息，因为在conn.open执行后，conn.Errors.Count就等于1，下面一段就是测试代码
'if sql="" then conn.errors.clear
' if conn.errors.count>0 then
  ' response.write "数据库连接对象有错了，看看是哪一步出的错："&"<br>"
  ' for i =0 to conn.errors.count-1
    ' response.write conn.errors.item(i)&"<br>"
  ' next
' else
  ' response.write "很完美!"
' end if
dim json
set json=jsObject()
if conn.Errors.Count=0 then
    '如果没有conn错误，则执行事务提交
    conn.CommitTrans
    if sql="" then 
        json("bStatus")=false
        json("sInfo")="没有进行任何操作"
    else
        json("bStatus")=true
    end if
else
    '否则回滚
    conn.RollbackTrans
    json("bStatus")=false
    json("sInfo")="数据库错误"
end if
json.Flush
%>
<!--#include file="lib/connclose.asp"-->

